package cn.community.care.module.system.service.materialsinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.community.care.module.system.controller.admin.materialsinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.materialsinfo.MaterialsinfoDO;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.util.object.BeanUtils;

import cn.community.care.module.system.dal.mysql.materialsinfo.MaterialsInfoMapper;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 物资基本信息 Service 实现类
 *
 * @author 美妆管理员
 */
@Service
@Validated
public class MaterialsinfoServiceImpl implements MaterialsinfoService {

    @Resource
    private MaterialsInfoMapper materialsinfoMapper;

    @Override
    public Long createMaterialsinfo(MaterialsinfoSaveReqVO createReqVO) {
        // 插入
        MaterialsinfoDO materialsinfo = BeanUtils.toBean(createReqVO, MaterialsinfoDO.class);
        materialsinfoMapper.insert(materialsinfo);
        // 返回
        return materialsinfo.getId();
    }

    @Override
    public void updateMaterialsinfo(MaterialsinfoSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialsinfoExists(updateReqVO.getId());
        // 更新
        MaterialsinfoDO updateObj = BeanUtils.toBean(updateReqVO, MaterialsinfoDO.class);
        materialsinfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialsinfo(Long id) {
        // 校验存在
        validateMaterialsinfoExists(id);
        // 删除
        materialsinfoMapper.deleteById(id);
    }

    private void validateMaterialsinfoExists(Long id) {
        if (materialsinfoMapper.selectById(id) == null) {
//            throw exception(MATERIALSINFO_NOT_EXISTS);
        }
    }

    @Override
    public MaterialsinfoDO getMaterialsinfo(Long id) {
        return materialsinfoMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialsinfoDO> getMaterialsinfoPage(MaterialsinfoPageReqVO pageReqVO) {
        return materialsinfoMapper.selectPage(pageReqVO);
    }

}